package views;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import presenters.MainListPresenter;

import com.example.wheresmystuff.R;

import data.DBInterfaceError;
import data.GetItemList;
import data.Item;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * activity for main list
 * 
 * @author Miles Raphael
 */
public class MainListActivity extends Activity implements IListView {

	/** presenter for main list */
	private MainListPresenter myPresenter;

	/** list of items */
	private ListView itemList;

	@Override
	/**
	 * gets called when the activity is created
	 * @param - the bundle
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_list);
		itemList = (ListView) findViewById(R.id.listView1);

		updateItemList();

		myPresenter = new MainListPresenter(this);
	}

	/**
	 * Updates the item list
	 */
	@SuppressLint("NewApi")
	public void updateItemList() {
		List<Item> items = null;
		try {
			GetItemList getItems = new GetItemList(this);
			items = getItems.getResult();
		} catch (DBInterfaceError e) {
			e.printStackTrace();
		}
		System.out.println(items + "items are here");

		List<String> itemStrs = new ArrayList<String>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				itemStrs.add(items.get(i).toString());
			}
		}

		String[] values = Arrays.copyOf(itemStrs.toArray(), itemStrs.size(),
				String[].class); // this shit is crazy because you can't cast a
									// Object[] array to at String[] array
		// http://www.vogella.com/articles/AndroidListView/article.html
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, // this is from android
				android.R.id.text1, // i don't know what this does
				values);
		itemList.setAdapter(adapter);
	}

	/**
	 * gets called when the menu is clicked
	 * 
	 * @param menu
	 *            - the menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lost_item_list, menu);
		return true;
	}

	/**
	 * gets called onFilterClick, opens filter options
	 * 
	 * @param source
	 *            - the source
	 */
	public void onFilterClick(View source) {
		myPresenter.onFilterClick(source);
	}

	/**
	 * gets called onSubmitClick, also updates the itemlist
	 * 
	 * @param source
	 *            - the source
	 */
	public void onSubmitClick(View source) {
		myPresenter.onSubmitClick(source);
	}

	/**
	 * gets called onRefreshClick
	 * 
	 * @param source
	 *            - the source
	 */
	public void onRefreshClick(View source) {
		updateItemList();
	}

	/*
	 * public void onWindowFocusChanged(boolean has) { if (has)
	 * updateItemList(); }
	 */

	/**
	 * advances
	 * 
	 * @param source
	 *            - the source
	 */
	@Override
	public void advance(View source) {
		Intent intent;
		switch (source.getId()) {
		case R.id.button1:
			intent = new Intent(this, SubmitItemActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.button4:
			intent = new Intent(this, FilterActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}

	/**
	 * updates the list - the source of the updater
	 */
	@Override
	public void update(View source) {
		updateItemList();

	}
}